global data /bplimext/projects/p064_MathildeMunoz/initial_dataset
global outputs /bplimext/projects/p064_MathildeMunoz/work_area
global list 33120 33200 41200 42110 42120 42130 42210 42220 42910 42990 ///
43110 43120 43130 43210 43221 43222 43290 43310 43320 43330 43340 43390 43910 43992 49410 78200 

clear 


*****************************************************************
* Loads raw firm-level data (CBHP) from BPLIM directory; 2006-2017
*****************************************************************


use "$data/PE064_CBHP_A_YFRM_20062017_JUN19_ROSTO_V01.dta", clear
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_MG_V01.dta"
drop _m
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_PESSOAL_V01.dta"
drop _m
g export_service_EU=0
replace export_service_EU=1 if MG005!=0
bys ano: tab export_service_EU
bys ano: tab exporta
g export_all=MG004+MG007
g export_sales_EU=0
replace export_sales_EU=1 if export_all!=0
bys ano: tab export_sales_EU
g postable=0
foreach y in $list {
replace postable=1 if cae3==`y'
}
merge 1:1 ano tina using "$data/PE064_CBHP_A_YFRM_20062017_JUN19_CONTAS_V01.dta"
drop _m
g tempvar=string(cae3, "%3.0g")
g gp=substr(tempvar,1,1)
destring gp, replace
g manufacturing=0
replace manufacturing=1 if gp==2
replace manufacturing=1 if gp==1

g type="."
replace type="manufacturing" if manufacturing==1
replace type="postable" if postable==1

keep tina ano cae3 D001 MG005 MG004 MG008 MG007 E001 type
egen tot_emp=sum(E001), by(ano)
egen tot_ca=sum(D001), by(ano)
g indic_g=0
replace indic_g=1 if MG004>0
g indic_s=0
replace indic_s=1 if MG005>0
g freq=1
sort ano
collapse (sum) freq indic_s indic_g MG004 MG005 MG007 MG008 E001 D001 (mean) tot_emp tot_ca, by(cae3 ano type)
g exporters_s=indic_s/freq
g exporters_g=indic_g/freq
g goods=MG004/D001
g services=MG005/D001

g label="."
replace label="Residential Construction Services" if cae3==41200
replace label="Roads Construction" if cae3==42110
replace label="Railways Construction" if cae3==42120
replace label="Bridges Construction" if cae3==42130
replace label="Utility Construction" if cae3==42210
replace label="Water Infrastructure Construction" if cae3==42990
replace label="Electrical Installation" if cae3==43210
replace label="Plumbing Installation" if cae3==43221
replace label="Other Construction Installation" if cae3==43290
replace label="Plastering" if cae3==43310
replace label="Joinery Installation" if cae3==43320
replace label="Floors and Wall Covering" if cae3==43330
replace label="Painting" if cae3==43340
replace label="Other Building Completion" if cae3==43390
replace label="Roofing Activities" if cae3==43910
replace label="Other Specialised Construction" if cae3==43992
replace label="Road Transport Services" if cae3==49410
replace label="Temporary Employment Agencies" if cae3==78200

*****************************************************************

******************************
*Figure 4
******************************

graph hbar services exporters_s goods if ano==2013 & label!="." & services>0.03 & type=="postable", ///
over(label, sort(exporters_s)  label(labsize(medsmall)))  graphregion(fcolor(white) lcolor(white)) ///
legend(label(1 "Export of Services/Turnover") ///
 label(3 "Export of Goods/Turnover") label(2 "Share of Exporting Firms") cols(1)  size(*0.8) ring(0) pos(1)) ///
 ytitle("") bargap(7) bar(1, fcolor(eltblue)) bar(2, fcolor(pink)) bar(3, fcolor(gs10))
graph export "$outputs/ES/Baseline_Export5/Figures/internationalintegration.png", replace


*****************************************************************



*few more descriptives 

replace label="Manufacture of outerwear" if cae3==14131
replace label="Manufacture of footwear" if cae3==15201
replace label="Manufacture of bread" if cae3==10711
replace label="Manufacture of other parts of motor vehicles" if cae3==29320
replace label="Manufacture of metal structures" if cae3==25110
replace label="Manufacture of doors and windows" if cae3==25120
replace label="Manufacture of plastic products" if cae3==22292
replace label="Other printings" if cae3==18120
replace label="Manufacture of metal moulds" if cae3==25734
replace label="Manufacture of underwear" if cae3==14140
replace label="Mixed farming" if cae3==01500
replace label="Manufacture of wine" if cae3==11021
replace label="Manufacture of textile" if cae3==13920
replace label="Other metal products" if cae3==25992
replace label="Manufacture of pastries" if cae3==10712
replace label="Manufacture of motor vehicles" if cae3==29100
replace label="Cotton=type weaving" if cae3==13201
replace label="Processing and preserving of meat" if cae3==23701
replace label="Computer Services" if cae3==62020


g all=.
replace all=MG004 if cae3==11021
replace all=MG005 if type=="postable"
replace all=MG005 if cae3==62020
g construction=0 
replace construction=1 if cae3==41200 | cae3==43990 | cae3==43992
egen toti=sum(MG005), by(construction ano)
replace all=toti if construction==1
keep if ano==2013
replace all=all/1000000


*Figure C20 

graph hbar all if cae3==11021 | cae3==49410 | cae3==78200 | cae3==41200 | cae3==62020, over(label, label(labsize(small))) ///
graphregion(fcolor(white) lcolor(white)) ytitle("Exports to EU (million euros)")
graph export "$outputs/ES/Baseline_Export5/Figures/exports.png", replace



graph hbar all if cae3==11021 | cae3==49410 | cae3==78200 | cae3==41200 | cae3==62020, over(label, label(labsize(small))) ///
graphregion(fcolor(white) lcolor(white)) ytitle("Exports to EU (million euros)") ///
 title("Portuguese Exports to EU, 2017", size(medsmall))
graph export "$outputs/ES/Baseline_Export5/Figures/exports_slides.png", replace



graph hbar services exporters_g goods if ano==2013 & label!="." & goods>0.03 & type=="manufacturing", ///
over(label, sort(exporters_s)  label(labsize(medsmall)))  graphregion(fcolor(white) lcolor(white)) ///
legend(label(1 "Export of Services/Turnover") ///
label(3 "Export of Goods/Turnover") label(2 "Share of Exporting Firms") cols(1)  size(*0.8) ) ///
ytitle("") bargap(7) bar(1, fcolor(eltblue)) bar(2, fcolor(pink)) bar(3, fcolor(gs10))





